<template lang="pug">
  .page
    .AccountsReceivableManage
      .search-form.mb-10.pl-10.pr-10
        Form.ivu-row.ivu-form-basic(ref="formData" :model="formData" :rules="rules" :label-width="70")
          FormItem.form-item.ivu-col.pr-20.mb-15(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'select'")
              Select(v-model="formData[item.model]" :placeholder="item.placeholder" clearable filterable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.label"
                  :key="index"
                ) {{ c_item.label }}
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder")
            template(v-if="item.type === 'button'")
              Button(type="info" @click="searchFn") 查询

      .pl-10.pr-10
        Tabs.ivu-tabs-nobg(@on-click="handleTabsClick" type="card")
          TabPane(v-for="tab in tabs" :label="tab.label" :name="tab.key" :animated="false")

        .pl-20.pr-20.bg-white
          Table.beautiful-table(:columns="setThead" :data="tableData" :stripe="true" ref="demoTable" @on-selection-change="handleOnSelectChange")
            .clearfix.font-16(slot="header")
              .table-title-lefttxt.pull-left
                |合计
                span.text-info.pl-5.pr-5.font-weight {{tableComone.totalRows}}
                |条
            .clearfix.pl-20.pr-20(slot="footer")
              Page(
                :current="tableComone.pageIndex"
                :total="tableComone.totalRows"
                @on-change="onChangePage"
                @on-page-size-change="onpageSizeChange"
                show-sizer
                show-total
                placement="top"
                prev-text="上一页"
                next-text="下一页"
                :page-size-opts="[10, 20, 30, 50, 100]"
              )

</template>

<script>
import { Common } from '@/utils/common.js'
import { PositionApi } from '@/api/Position.api.js'
import { PositionSummaryInfoThead, EmployeeSummaryInfoThead } from './tableHead.js'
import _cloneDeep from 'clone-deep'

export default {
  name: '', // 组织信息汇总

  components: {
  },

  data () {
    return {
      companyId: '', // 公司ID
      companyName: '', // 公司名称
      employeeId: '', // 员工ID
      positionSummaryInfoThead: _cloneDeep(PositionSummaryInfoThead),
      employeeSummaryInfoThead: _cloneDeep(EmployeeSummaryInfoThead),
      tableData: [],
      selectedId: [], // 勾选的id集合,array格式
      selectedArr: [], // 勾选的id, name数组集合
      tableComone: {
        pageIndex: 1,
        totalRows: 0,
        pageSize: 10
      },
      formData: {
        name: ''
      },
      formItem: [
        { type: 'input', label: '职位名称：', placeholder: '请输入职位名称', prop: 'name', model: 'name', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-9 ivu-col-span-xxl-6' },
        { type: 'button', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-3 ivu-col-span-xxl-3' }
      ],
      rules: {},
      arrListDown: {
        tempOps: []
      },
      importFile: null,
      tabs: [
        { label: '职位信息', key: 'getList' },
        { label: '员工信息', key: 'getUserList' }
      ],
      currentTabsName: 'getList'
    }
  },

  computed: {
    setThead () {
      if (this.currentTabsName === 'getList') {
        return this.positionSummaryInfoThead
      } else {
        return this.employeeSummaryInfoThead
      }
    }
  },

  mounted () {
    this.query()
  },

  methods: {
    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1

      if (this.currentTabsName === 'getList') {
        this.query()
      } else {
        this.queryUserList()
      }
    },

    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      console.log('💗 val: ', val)
      this.tableComone.pageIndex = val

      if (this.currentTabsName === 'getList') {
        this.query()
      } else {
        this.queryUserList()
      }
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      console.log('⭐ val: ', val)
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val

      if (this.currentTabsName === 'getList') {
        this.query()
      } else {
        this.queryUserList()
      }
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        Name: this.formData.name,
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await PositionApi.getList(params)
      console.log('发票开具汇总res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
      } else {
        this.tableData = []
      }
      this.loading = false
    },

    async queryUserList () {
      var params = {
        PositionName: this.formData.name,
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await PositionApi.getUserList(params)
      console.log('获取列表res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
      } else {
        this.tableData = []
      }
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedArr = value.map((item) => {
        return { id: item.companyID, name: item.invoiceItem }
      })
      this.selectedId = value.map((item) => {
        return item.companyID
      })
      console.log('列表勾选数组1', this.selectedArr)
      console.log('列表勾选数组2', this.selectedId)
    },

    // tab切换
    handleTabsClick (name) {
      this.currentTabsName = name
      console.log('name: ', name)
      this.searchFn()

      console.log('xxxx this.tableComone: ', this.tableComone)
    }
  } // methods end
}
</script>

<style lang="less" scoped>

</style>
